import {Component, OnInit, Input, EventEmitter, Output} from '@angular/core';
import {SelectRoleService} from './select-role.service';
import {NzMessageService} from 'ng-zorro-antd';

@Component({
    selector: 'app-select-role',
    templateUrl: './select-role.component.html',
    styleUrls: ['./select-role.component.less'],
    providers: [SelectRoleService]
})
export class SelectRoleComponent implements OnInit {
    @Input() dryPersonnel;
    @Input() roleList;
    @Input() requestHttp = '/role/userListByRole';
    @Input() roleType;
    @Output() roleChang = new EventEmitter<any>();
    @Output() cancelPersonnel = new EventEmitter<any>();
    @Output() selectPersonel = new EventEmitter<any>();
    radioValue;

    constructor(private selectrole: SelectRoleService,
                private message: NzMessageService) {

    }

    ngOnInit() {
        // debugger
        if (this.roleType !== undefined && this.roleType !== '') {
            this.getRole();
        }
    }

    getRole() {
        this.selectrole.getRoleList(this.requestHttp, this.roleType)
            .then(res => {
                if (res.status === 200) {
                    this.dryPersonnel = res.result;
                    this.radioValue = this.dryPersonnel[0].userNo;
                } else {
                    this.message.create('error', res.message);
                }
            })
            .catch(err => {
                this.message.create('error', err);
            });
    }

    dryChang(e) {
        this.radioValue = e;
        this.roleChang.emit();
    }

    // 选择
    selectDryPersonnel() {
        this.selectPersonel.emit(this.radioValue);
    }

    // 取消
    cancelDryPersonnel() {
        this.radioValue = '';
        this.cancelPersonnel.emit();
    }
}
